package com.yu;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.yu.dao.UserDao;
import com.yu.pojo.User;
import org.junit.jupiter.api.Test;
import org.springframework.boot.test.context.SpringBootTest;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

/**
 * @author DongYu Zhou
 * @date 2022/6/9 14:12
 */
@SpringBootTest
public class SpringBootMpWarpper {

    @Resource
    private UserDao userDao;

    //条件查询封装的条件都是 数据库中的表名字段 不是实体类的字段名

    @Test
    public void selByWarpper() {
//        场景 查询条件是 name包含a  年龄在20-30之间 email不为空

        //条件构造器
        QueryWrapper<User> userQueryWrapper = new QueryWrapper<>();
        userQueryWrapper.like("name", "a")
                .between("age", 20, 30)
                .isNotNull("email");

        userDao.selectList(userQueryWrapper);

    }

    @Test
    public void selByOrder() {
//        场景 按照年龄降序排名 年龄一样按照id升序

        //条件构造器
        QueryWrapper<User> userQueryWrapper = new QueryWrapper<>();
        userQueryWrapper.orderByDesc("age")
                .orderByAsc("id");

        List<User> users = userDao.selectList(userQueryWrapper);
        for (User user : users) {
            System.out.println("user = " + user);
        }
    }

    @Test
    public void OrderDel() {
//        场景 删除Emile为空的用户

        //条件构造器
        QueryWrapper<User> userQueryWrapper = new QueryWrapper<>();
        userQueryWrapper.isNull("email");
        int delete = userDao.delete(userQueryWrapper);
        System.out.println("delete = " + delete);
    }

    @Test
    public void t1() {
//        场景 修改name包含雨的and age=18 or email is null

        //条件构造器
        QueryWrapper<User> userQueryWrapper = new QueryWrapper<>();
        userQueryWrapper.like("name", "雨")
                .eq("age", 18)
                .or()
                .isNull("email");
        User user = new User();
        user.setName("玛玛哈哈");
        user.setEmail("150");
        int update = userDao.update(user, userQueryWrapper);
        System.out.println("update = " + update);
    }

    @Test
    public void t2() {
        //将用户名中包含有a并且（年龄大于20或邮箱为null）的用户信息修改
        QueryWrapper<User> userQueryWrapper = new QueryWrapper<>();
        userQueryWrapper.like("name", "a").and(i -> {
            i.gt("age", 20).or().isNull("email");
        });
        User user = new User();
        user.setName("橘桑");
        user.setEmail("150");
        userDao.update(user, userQueryWrapper);

    }

    @Test
    public void t3() {
        //查询用户(name,age,email)
        //条件构造器
        QueryWrapper<User> userQueryWrapper = new QueryWrapper<>();
        userQueryWrapper.select("name", "age", "email");
        List<Map<String, Object>> mapList =
                userDao.selectMaps(userQueryWrapper);
        mapList.forEach(System.out::println);
    }

    @Test
    public void t4() {
        //使用子查询 查询id<100 的用户
        QueryWrapper<User> userQueryWrapper = new QueryWrapper<>();
        userQueryWrapper.inSql("id", "select id from t_user where id<=100");
        List<User> users = userDao.selectList(userQueryWrapper);
        users.forEach(System.out::println);
    }
    @Test
    public void t5() {
        //将用户名中包含有i并且（年龄大于20或邮箱为null）的用户信息修改

        /**
         * 使用UpdateWrapper<>(); 设置条件  还可以的设置修改的字段
         */
        UpdateWrapper<User> userUpdateWrapper = new UpdateWrapper<>();
        userUpdateWrapper.like("name","i").and(i->{
            i.gt("age",20).or().isNull("email");
        });
        userUpdateWrapper.set("name","硅谷学子");
        userUpdateWrapper.set("email","200");
        int update = userDao.update(null, userUpdateWrapper);
        System.out.println("update = " + update);
    }

    @Test
    public void t6() {
        /**
         * 设置条件  组装条件
         */
        String name="";
        Integer ageBeg=18;
        Integer ageEnd=30;
        QueryWrapper<User> userQueryWrapper = new QueryWrapper<>();
        if (StringUtils.isNotBlank(name)){
            userQueryWrapper.like("name",name);
        }
        if (ageBeg!=null){
            userQueryWrapper.gt("age",ageBeg); //年龄在什么之间 开始
        }
        if (ageEnd!=null){
            userQueryWrapper.lt("age",ageEnd);//年龄在什么之间 结束
        }

        List<User> users = userDao.selectList(userQueryWrapper);
        users.forEach(System.out::println);
    }

    @Test
    public void t7() {
        /**
         * 设置条件  组装条件
         */
        String name="哈";
        Integer ageBeg=18;
        Integer ageEnd=30;
        QueryWrapper<User> userQueryWrapper = new QueryWrapper<>();
       userQueryWrapper.like(StringUtils.isNotBlank(name),"name",name)
               .gt(ageBeg!=null,"age",ageBeg)
               .lt(ageEnd!=null,"age",ageEnd);

        List<User> users = userDao.selectList(userQueryWrapper);
        users.forEach(System.out::println);
    }

    @Test
    public void t8() {
        /**
         * 设置条件  组装条件 LambdaQueryWrapper
         */
        String name="哈";
        Integer ageBeg=18;
        Integer ageEnd=30;
        LambdaQueryWrapper<User> wrapper = new LambdaQueryWrapper<>();
        wrapper.like(StringUtils.isNotBlank(name),User::getName,name)
                .gt(ageBeg!=null,User::getAge,ageBeg)
                .lt(ageEnd!=null,User::getAge,ageEnd);

        List<User> users = userDao.selectList(wrapper);
        users.forEach(System.out::println);
    }
    @Test
    public void t9() {
        //将用户名中包含有i并且（年龄大于20或邮箱为null）的用户信息修改

        /**
         * 使用UpdateWrapper<>(); 设置条件  还可以的设置修改的字段
         */
        LambdaUpdateWrapper<User> wrapper = new LambdaUpdateWrapper<>();
        wrapper.like(User::getName,"i").and(i->{
            i.gt(User::getAge,20).or().isNull(User::getEmail);
        });
        wrapper.set(User::getName,"硅谷学子");
        wrapper.set(User::getEmail,"250");
        int update = userDao.update(null, wrapper);
        System.out.println("update = " + update);
    }
}
